feat(auth): migrate to better-auth admin plugin with unified Admin tab#3612
feat(auth): migrate to better-auth admin plugin with unified Admin tab#3612waleedlatif1 merged 12 commits intostagingfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
You have used all Bugbot PR reviews included in your free trial for your GitHub account on this workspace. To continue using Bugbot reviews, enable Bugbot for your team in the Cursor dashboard. |
Greptile SummaryThis PR migrates super-user management from a custom boolean column to better-auth's native admin plugin, replacing the old Key changes:
Issues found:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Browser
participant AdminUI as Admin Settings Tab
participant AuthAPI as better-auth admin endpoints
participant DB as PostgreSQL
Browser->>AdminUI: Navigate to settings/admin
AdminUI->>AdminUI: Check session user role
alt role is not admin
AdminUI->>Browser: Redirect to settings/general
end
Browser->>AdminUI: Click Load Users
AdminUI->>AuthAPI: listUsers with limit and offset
AuthAPI->>DB: Query user table
DB-->>AuthAPI: Return paginated rows
AuthAPI-->>AdminUI: Users array with total count
AdminUI-->>Browser: Render user management table
Browser->>AdminUI: Click Promote or Demote button
AdminUI->>AuthAPI: setRole with userId and target role
AuthAPI->>DB: Update role column
AuthAPI-->>AdminUI: Success
AdminUI->>AdminUI: Invalidate adminUsers cache
Browser->>AdminUI: Click Ban then enter reason then Confirm
AdminUI->>AuthAPI: banUser with userId and optional reason
AuthAPI->>DB: Set banned true and store reason
AuthAPI-->>AdminUI: Success
AdminUI->>AdminUI: Invalidate adminUsers cache
|
Consolidate superuser features into a single Admin settings tab: - Super admin mode toggle (moved from General) - Workflow import (moved from Debug) - User management via better-auth admin (list, set role, ban/unban) Replace Debug tab with Admin tab gated by requiresAdminRole. Add React Query hooks for admin user operations.
Add UPDATE statement to promote is_super_user=true rows to role='admin' before dropping the is_super_user column, preventing silent demotion.
- Fix cn import path to @/lib/core/utils/cn - Use valid Badge variants (blue/gray/red/green instead of secondary/destructive) - Type setRole param as 'user' | 'admin' union
Include user.role in customSession so it's available client-side. Replace all useSuperUserStatus() calls with session.user.role === 'admin'. Delete the now-redundant /api/user/super-user endpoint.
The admin plugin already includes role on the user object. No need to manually spread it in customSession.
… practices - Remove unsafe unknown/Record casting, use better-auth typed response - Add placeholderData: keepPreviousData for paginated variable-key query - Remove nullable types where defaults are always applied
|
@greptile |
apps/sim/app/workspace/[workspaceId]/settings/components/admin/admin.tsx
Show resolved
Hide resolved
apps/sim/app/workspace/[workspaceId]/settings/components/admin/admin.tsx
Outdated
Show resolved
Hide resolved
- Fix superUserModeEnabled default to false (matches sidebar behavior) - Reset banReason when switching ban target to prevent state bleed - Guard admin section render with session role check for direct URL access
|
@greptile |
Three places defaulted to true while admin tab and sidebar used false. Align all to false so new admins see consistent behavior.
|
You have used all Bugbot PR reviews included in your free trial for your GitHub account on this workspace. To continue using Bugbot reviews, enable Bugbot for your team in the Cursor dashboard. |
|
@greptile |
apps/sim/app/workspace/[workspaceId]/settings/components/admin/admin.tsx
Outdated
Show resolved
Hide resolved
apps/sim/app/workspace/[workspaceId]/settings/components/admin/admin.tsx
Show resolved
Hide resolved
|
@greptile |
apps/sim/app/workspace/[workspaceId]/settings/components/admin/admin.tsx
Show resolved
Hide resolved
apps/sim/app/workspace/[workspaceId]/settings/components/admin/admin.tsx
Show resolved
Hide resolved
…edback - Only read mutation.variables when mutation isPending (prevents stale ID) - Add isPending guard to super user mode toggle (prevents concurrent mutations) - Show inline error message when setRole/ban/unban mutations fail
|
You have used all Bugbot PR reviews included in your free trial for your GitHub account on this workspace. To continue using Bugbot reviews, enable Bugbot for your team in the Cursor dashboard. |
|
@greptile |
apps/sim/app/workspace/[workspaceId]/settings/components/admin/admin.tsx
Show resolved
Hide resolved
…in blocking - Replace pendingUserId scalar with pendingUserIds Set (useMemo) so concurrent mutations across different users each disable their own row correctly - Add sessionLoading guard to admin section redirect to prevent flash on direct /settings/admin navigation before session resolves - Add BLOCKED_SIGNUP_DOMAINS env var and before-hook for email domain denylist, parsed once at module init as a Set for O(1) per-request lookups - Add trailing newline to migration file Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@greptile |
apps/sim/app/workspace/[workspaceId]/settings/components/admin/admin.tsx
Show resolved
Hide resolved
…icon - Add databaseHooks.user.create.before to enforce BLOCKED_SIGNUP_DOMAINS at the model level, covering all signup vectors (email, OAuth, social) not just /sign-up paths - Call .reset() on each mutation before firing to clear stale error state from previous operations - Change Admin nav icon from ShieldCheck to Lock to avoid duplicate with Access Control tab Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Summary
isSuperUserboolean with better-auth's native admin plugin (role,banned,banReason,banExpireson user table,impersonatedByon session table)/api/user/super-userAPI route — role now available directly from session viacustomSession+ admin pluginis_super_user = truerows torole = 'admin'before dropping the columnChanges
isSuperUser→roletext field +banned/banReason/banExpirescolumns,impersonatedByon sessionadmin()server plugin andadminClient()client pluginuser.roleexposed via better-auth'scustomSession— no custom override neededuseAdminUsers,useSetUserRole,useBanUser,useUnbanUser) following project patternsrequiresAdminRolegate (checks role only, not mode toggle) so admin tab stays accessibleuseSuperUserStatushookTest plan
user.rolefield after login